﻿#include "myoffice.h"
#include <QTextDocument>
#include <QTextBlock>
#include <QString>
#include <QDateTime>
#include <QTextStream>
#include <QFileInfo>
#include "systempreset.h"
#include "commonfunction.h"
#include "qdebug.h"

MyOffice::MyOffice(QObject *parent) : QObject(parent)
{
    initWidget();
}

void MyOffice::generateHtml()
{
    generateHeaderHtml();
    generateTailHtml();
    allHtml= "";
    allHtml += headerHtml;
    allHtml += contentHtml;
    allHtml += tailHtml;
}

void MyOffice::generateHeaderHtml()
{
    if(companyChecked)
    {
        companyName = QString(tr("Company: ")) + QString(tr("Etta Biotech"));
    }
    else {
        companyName = "";
    }
    if(equipmentChecked)
    {
        equipName = QString(tr("Equipment: ")) + QString(tr("EttaEquipment"));
    }
    else {
        equipName = "";
    }
    if(serialChecked)
    {
        //todo:get equipment serial
        equipSerial= QString(tr("EquipSerial:")) + "Etta123456";
    }
    else {
        equipSerial="";
    }
    headerHtml = "";
    headerHtml += "<style>table tr td{text-align:center;}</style><meta charset='utf-8'>";
    headerHtml += "<h1 align='center'>" + QString(tr("Etta Report")) + "</h1>";
    headerHtml += "<p>";
    headerHtml += companyName;
    headerHtml += "&#9;";//Tab "&#9;"
    headerHtml += equipName;
    headerHtml += "&#9;";//Tab "&#9;"
    headerHtml += equipSerial;
    headerHtml += "</p><p></p>";
}

void MyOffice::generateTailHtml()
{

    if(datetimeChecked)
    {
        datetime = QString(tr("Datetime:")) + currentDateTime();
    }
    else {
        datetime = "";
    }

    if(userChecked)
    {
        username = QString(tr("User:")) + currentUsername;
    }
    else {
        username = "";
    }

    tailHtml = "";  //reset
    tailHtml += "<meta charset='utf-8'>";
    tailHtml += "<p>";
    tailHtml += datetime;
    tailHtml += "&#9;"; //Tab
    tailHtml += username;
    tailHtml += "</p>";
}

void MyOffice::convert2Doc(const QString &fileName)
{
    QFile outFile(fileName);
    outFile.open(QIODevice::Truncate |QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
    ts << allHtml << endl;
}

void MyOffice::convert2PDF(const QString &fileName)
{
    QPrinter printerHtml;
    printerHtml.setPageSize(pageSize);
    printerHtml.setOutputFormat(QPrinter::PdfFormat);
    printerHtml.setOutputFileName(fileName);
    QTextDocument text_document;
    text_document.setHtml(allHtml);
    text_document.print(&printerHtml);
    text_document.end();
}

void MyOffice::save2PDF_Doc(const QString& fileName)
{
    QFileInfo fileInfo(fileName);
    QString suffix = fileInfo.suffix();
    if (suffix.contains("doc") || suffix.contains("docx"))
    {
        convert2Doc(fileName);
    }
    else if (suffix.contains("pdf"))
    {
        convert2PDF(fileName);
    }
    else {
        return;
    }
}

void MyOffice::savePdf(const QString &fileName, const QString &_contentHtml)
{
    contentHtml = _contentHtml;
    generateHtml();
    convert2PDF(fileName);
}

void MyOffice::savePdfSlot(int type, const QString &fileName, const QString &_contentHtml)
{
    contentHtml = _contentHtml;
    generateHtml();
    convert2PDF(fileName);
    emit savePdfSucceedSignal(type);
}

void MyOffice::updatePrintParamSlot()
{
    readParam();
}

void MyOffice::generateAllHtml(int type, const QString _contentHtml)
{
    contentHtml = _contentHtml;
    generateHtml();
    qDebug() << allHtml;
    emit generateAllHtmlSucceedSignal(type, allHtml);
}

void MyOffice::initWidget()
{
    companyName = tr(SystemPreset::instance()->getStringData(EnumPresetItem::CompanyName).toStdString().data());
    equipName = tr(SystemPreset::instance()->getStringData(EnumPresetItem::InstrumentName).toStdString().data());
    equipModel = SystemPreset::instance()->getStringData(EnumPresetItem::InstrumentType);
    equipSerial = SystemPreset::instance()->getStringData(EnumPresetItem::SerialNumber);
    username = currentUsername;
    datetime = currentDateTime();

    headerHtml = "";
    contentHtml = "";
    tailHtml = "";
    allHtml = "";
    pageSize = QPrinter::A4;
    readParam();
}

void MyOffice::readParam()
{
    companyChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::CompanyChecked);
    equipmentChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::EquipmentChecked);
    serialChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::SerialChecked);
    userChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::UserChecked);
    datetimeChecked = SystemPreset::instance()->getBoolData(EnumPresetItem::DatetimeChecked);
    pageSize = static_cast<QPrinter::PageSize>(SystemPreset::instance()->getIntData(EnumPresetItem::PageSize));
}
